/*
 * @Author: xiehangyun 
 * @Date: 2025-05-16 10:34:07 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2025-09-16 11:25:09
 */

import Taro from "@tarojs/taro";
import { makeAutoObservable } from "mobx";
import utilsApi from "src/api/utilsApi";
import myPromotionApi from "./myPromotionApi";

class MyPromotionStore {
    constructor() {
        makeAutoObservable(this, {}, { deep: false });

        // autorun(() => {
        //     if (userStore.isLogin) {
        //         this.createPromotionCode();
        //         this.queryUserIncomeMap();
        //     }
        // }, { delay: 30 });

    }

    consumerThresholdImg: string = "";//消费门槛图片
    promotionCode: string = "";//推广码
    hasParent: boolean = false; // 是否有父推广码
    requiredAmount: number = 0; // 申请推广码的消费门槛
    validPaidAmount: number = 0; // 已完成的有效推广码的消费金额
    createPromotionCode() {
        return myPromotionApi.createPromotionCode().then((res: any) => {
            this.promotionCode = res.msg;
            this.hasParent = res.hasParent;
            this.requiredAmount = res.requiredAmount;
            this.validPaidAmount = res.validPaidAmount;

            //如果 res.msg 里面 包含  申请失败 字样
            if (res.msg.indexOf("申请失败") !== -1) {
                utilsApi
                    .queryPropertyByTitle("consumer_threshold_img")
                    .then((data) => {
                        // console.log("data", data);
                        this.consumerThresholdImg = data;
                    });
            }
            // 关闭 Taro.showToast
            Taro.hideToast();
            return res;
        })
    }
    clearPromotionCode() {
        this.promotionCode = "";
    }

    // 当前收益金额
    incomeMoney = 0;
    // 未完成收益金额
    unfinishedMoney = 0;
    // 昨日收益金额
    yesterdayMoney = 0;
    //查询当前登陆用户收益明细
    queryUserIncomeMap() {
        myPromotionApi.queryUserIncomeMap().then((res: any) => {
            this.incomeMoney = res.data.incomeMoney || 0;
            this.unfinishedMoney = res.data.unfinishedMoney || 0;
            this.yesterdayMoney = res.data.yesterdayMoney || 0;
        });
    }




}

const myPromotionStore = new MyPromotionStore();

export default myPromotionStore;